package com.taoyibao.hongcanting.ui.home.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.activity.BaseActivityPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.event.EventUpdataCart;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.DishModel;
import com.taoyibao.hongcanting.model.SupplementModel;
import com.taoyibao.hongcanting.ui.home.delegate.ChooseAddDelegate;
import com.taoyibao.hongcanting.utils.LogUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * @author TangAnna
 * @description: 添加菜品之前选择口味的页面
 * @date :${DATA} 13:48
 */
public class ChooseAddActivity extends BaseActivityPresenter<ChooseAddDelegate> implements View.OnClickListener {
    private DishModel mDishModel;
    private SupplementModel mSupplementModel;

    @Override
    protected Class<ChooseAddDelegate> getDelegateClass() {
        return ChooseAddDelegate.class;
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        mDishModel = (DishModel) getIntent().getSerializableExtra(CodeConstant.CURRENT_DISH);
        LogUtils.dNormal("TAG", "选择页面收到的菜品信息" + mDishModel.toString());
        mSupplementModel = (SupplementModel) getIntent().getSerializableExtra(CodeConstant.DISH_SUPPLEMENT);
        viewDelegate.setChooseData(mSupplementModel);
        viewDelegate.setDishData(mDishModel);
        viewDelegate.get(R.id.btn_chooseAdd_confirm).setOnClickListener(this);
    }

    /**
     * @param context
     * @param dishModel 当前菜品
     */
    public static void open(Context context, DishModel dishModel) {
        Intent intent = new Intent(context, ChooseAddActivity.class);
        intent.putExtra(CodeConstant.CURRENT_DISH, dishModel);
        context.startActivity(intent);
    }

    /**
     * @param context
     * @param dishModel       当前菜品
     * @param supplementModel 菜品的可选择口味
     */
    public static void open(Context context, DishModel dishModel, SupplementModel supplementModel) {
        Intent intent = new Intent(context, ChooseAddActivity.class);
        intent.putExtra(CodeConstant.CURRENT_DISH, dishModel);
        intent.putExtra(CodeConstant.DISH_SUPPLEMENT, supplementModel);
        context.startActivity(intent);
    }

    /**
     * @param context
     * @param supplementModel 菜品的可选择口味
     */
    public static void open(Context context, SupplementModel supplementModel) {
        Intent intent = new Intent(context, ChooseAddActivity.class);
        intent.putExtra(CodeConstant.DISH_SUPPLEMENT, supplementModel);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_chooseAdd_confirm://确定
                if (!TextUtils.isEmpty(viewDelegate.getChooseTaste()) && !TextUtils.isEmpty(viewDelegate.getChooseMakeUp())) {
                    addDish();
                }
                break;
        }
    }

    /**
     * 添加菜品
     */
    public void addDish() {
        mDishModel.dish_count++;
        mDishModel.makeup = viewDelegate.getChooseMakeUp();
        mDishModel.taste = viewDelegate.getChooseTaste();

        String class_code = "";
        if (!TextUtils.isEmpty(mDishModel.dish_classId)) {
            class_code = mDishModel.dish_classId;
        } else if (!TextUtils.isEmpty(mDishModel.class_code)) {
            class_code = mDishModel.class_code;
        }
        JsonApiWrapper.getApiService().cartAdd(SpUtils.getToken(), SpUtils.getMemberId(), class_code, mDishModel.class_name,
                mDishModel.dish_code, mDishModel.dish_pic, mDishModel.dish_name, "1",
                mDishModel.dish_unit, mDishModel.makeup, mDishModel.taste, mDishModel.dish_price + "",
                mDishModel.dish_discount_price + "")
                .compose(new DefaultTransformer<CompanyBaseModel>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel>(this, true) {
                    @Override
                    public void onNext(CompanyBaseModel updataCartModel) {
                        if (updataCartModel.code == NetCode.HTTP_200) {
                            ToastUtils.showToast("添加成功");
                            sendEvent();
                        } else {
                            super.onNext(updataCartModel);
                        }
                        finish();
                    }
                });
    }

    /**
     * 添加成功之后发送消息通知菜单页面更新
     */
    public void sendEvent() {
        EventUpdataCart eventUpdataCart = new EventUpdataCart();
        eventUpdataCart.source = CodeConstant.EVENT_SOURCE_MENU;
        eventUpdataCart.action = 1;
        eventUpdataCart.dishModel = mDishModel;
        EventBus.getDefault().post(eventUpdataCart);
    }
}
